﻿using CursesSharp;
using habbo.Habbo.Catalog;
using habbo.Habbo.Characters;
using habbo.Kernel.Commons;
using habbo.Kernel.IO;
using habbo.Kernel.Network;
using habbo.Kernel.Packets;
using HabboEnvironment_R3.Kernel.IO;
using HabboEnvironment_R3.Kernel.Network;
using HabboEnvironment_R3.Kernel.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace habbo
{
    static class SystemApp
    {
        public static ConsoleSystem ConsoleSystem;
        public static SocketSystem SocketSystem;
        public static PacketManager PacketManager;
        public static MySQLManager MySQLManager;
        public static CatalogManager CatalogManager;
        public static CharacterManager CharacterManager;
        public static Habbo.HabboSystem HabboSystem;
        public static Configuration Config;
        public static MusListener MusListener
        {
            get { return MusListener.GetMusListener(30001); }
        }

        private static readonly Dictionary<string, List<string>> BannerTokensHolder = new Dictionary<string, List<string>>();

        public static Dictionary<string, List<string>> BannerTokens
        {
            get { return BannerTokensHolder; }
        }

        static void Main(string[] args)
        {
            Console.WindowHeight = Console.LargestWindowHeight - 25;
            Console.WindowWidth = Console.LargestWindowWidth - 25;

            Config = new Configuration("config.conf");
            ConsoleSystem = new ConsoleSystem();
            SocketSystem = new SocketSystem();
            CharacterManager = new CharacterManager();
            CatalogManager = new CatalogManager();
            MySQLManager = new MySQLManager(Config.configData["db.host"], uint.Parse(Config.configData["db.port"]), Config.configData["db.username"], Config.configData["db.password"], Config.configData["db.name"], bool.Parse(Config.configData["db.pooling"]), uint.Parse(Config.configData["db.minpoolsize"]), uint.Parse(Config.configData["db.maxpoolsize"]));
            ConsoleSystem.Serialize("habbo - Sulake better get it coming!");
            ConsoleSystem.PrintLine("BOOT", @"   _           _     _           ");
            ConsoleSystem.PrintLine("BOOT", @"  | |         | |   | |          ");
            ConsoleSystem.PrintLine("BOOT", @"  | |__  _____| |__ | |__   ___    Welcome, {0}", Environment.UserName);
            ConsoleSystem.PrintLine("BOOT", @"  |  _ \(____ |  _ \|  _ \ / _ \  - sulake better get it coming");
            ConsoleSystem.PrintLine("BOOT", @"  | | | / ___ | |_) ) |_) ) |_| |  ["+VersionInfo.Version+"]");
            ConsoleSystem.PrintLine("BOOT", @"  |_| |_\_____|____/|____/ \___/");
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.Write(" <{0}> ", "BOOT");
            Console.ForegroundColor = ConsoleColor.White;
            for (int i = 0; i < Console.WindowWidth - 8; i++)
            {
                Console.Write("-");
            }
            Console.ResetColor();

            SocketSystem.ConstructSocket(IPAddress.Parse(Config.configData["socket.host"]), int.Parse(Config.configData["socket.port"]), int.Parse(Config.configData["socket.backlog"]));
            SocketSystem.ConstructPooling(int.Parse(Config.configData["socket.maxconnections"]));
            MusListener.Start();

            HabboSystem = new Habbo.HabboSystem();
            PacketManager = new PacketManager();

            while (true) Console.Read();
        }

        public static Double GetUnixTimestamp()
        {
            TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
            return ts.TotalSeconds;
        }
    }
}